package com.acg.controller;

import com.acg.entity.OrderItem;
import com.acg.service.OrderItemService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.*;
/**
 * 订单详情表(OrderItem)表控制层
 *
 * @author makejava
 * @since 2025-07-01 16:34:32
 */
@RestController
@RequestMapping("orderItem")
public class OrderItemController {
    /**
     * 服务对象
     */
    @Resource
    private OrderItemService orderItemService;

    /**
     * 分页查询
     *
     * @param orderItem 筛选条件
     * @return 查询结果
     */
    @RequestMapping(value="queryByPageOrderItem")
    public Map<String,Object> queryByPage(OrderItem orderItem, Integer page,Integer pagesize) {
        page=page==null||page<1?1:page;
        pagesize=pagesize==null||pagesize<1?5:pagesize;

        //获取分页的请求对象
        PageRequest pageRequest=PageRequest.of((page-1),pagesize);
        Page<OrderItem> orderItemPage=orderItemService.queryByPage(orderItem,pageRequest);

        //获取总页数
        int totalPage=orderItemPage.getTotalPages();
        //获取当前页的记录集合
        List<OrderItem> orderItemList=orderItemPage.getContent();

        //创建Map集合封装分页数据
        Map<String,Object> mappage=new HashMap<>();
        mappage.put("page",page);
        mappage.put("pagesize",pagesize);
        mappage.put("maxpage",totalPage);
        mappage.put("orderItemList",orderItemList);
        return mappage;
    }
    
    /**
    获取所有的数据
    */
    @RequestMapping(value="queryAllOrderItem")
    public List<OrderItem> queryAll(){
        return this.orderItemService.queryAll();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping(value="queryByIdOrderItem")
    public OrderItem queryById(Integer id) {
        return this.orderItemService.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param orderItem 实体
     * @return 新增结果
     */
    @RequestMapping(value="saveOrderItem")
    public int save(OrderItem orderItem) {
        if(this.orderItemService.insert(orderItem)!=null){
            return 1;
        }
        return 0;
    }

    /**
     * 编辑数据
     *
     * @param orderItem 实体
     * @return 编辑结果
     */
    @RequestMapping(value="updateOrderItem")
    public int update(OrderItem orderItem) {
         if(this.orderItemService.update(orderItem)!=null){
            return 1;
        }
        return 0;       
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
   @RequestMapping(value="deleteByIdOrderItem")
    public int deleteById(Integer id) {
        if(this.orderItemService.deleteById(id)){
            return 1;
        }
        return 0;
    }

}
